package com.hjl.error;

import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author ServivE
 * @version 0.1
 * @className GlobalExceptionHandler
 * @description 全局异常处理器
 * @date 2024/8/13 16:47
 * @since jdk17
 */
@RestControllerAdvice
public class GlobalExceptionHandler {
    //NullPointerException
    @ExceptionHandler(NullPointerException.class)
    public Object handlerNullPointerException(NullPointerException e) {
        String message = e.getMessage();
        System.out.println("message = " + message);
        return message;
    }

    //ArithmeticException:
    @ExceptionHandler(ArithmeticException.class)
    public Object handlerArithmeticException(ArithmeticException e) {
        String message = e.getMessage();
        System.out.println("message = " + message);
        return message;
    }
    /**
     * 所有异常都会触发此方法!但是如果有具体的异常处理Handler!
     * 具体异常处理Handler优先级更高!
     * 例如: 发生NullPointerException异常!
     *       会触发handlerNullException方法,不会触发handlerException方法!
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    public Object handlerException(Exception e){

        return null;
    }

}
